<%@page import="org.siscoh.model.dao.UsuarioDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="org.siscoh.model.bean.UsuarioBean"%>
<%@page import="org.siscoh.controller.ControlePermissao"%>
<%@page import="org.siscoh.controller.Constants"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    request.setAttribute("minper", 1);
    
    boolean editar = false, self = true, admin = false;
    String nome = "", login = "";
    String isAdminStr = "", notAdminStr = "CHECKED";
    
    if(!ControlePermissao.possuiPermissao(request, response))
    {
        %> <jsp:include flush="true" page="/403.html" />  <%
        return;
    }
    else if (request.getParameter("userid") != null)
    {
        UsuarioBean logado = (UsuarioBean) session.getAttribute("user");
        UsuarioBean usuario = (UsuarioBean) new UsuarioDao().selectById(Long.parseLong(request.getParameter("userid")));
        
        if(usuario.getId() != logado.getId())
        {
            self = false;
            if(!logado.isAdministrador())
            {
                %> <jsp:include flush="true" page="/403.html" />  <%
                return;
            }
        }
        
        nome = usuario.getNome();
        login = usuario.getLogin();
        
        if(usuario.isAdministrador())
        {
            isAdminStr = "CHECKED";
            notAdminStr = "";
            admin = true;
        }        
        
        editar = true;
    }
    else
    {
        request.setAttribute("minper", 2);
        if(!ControlePermissao.possuiPermissao(request, response))
        {
            %> <jsp:include flush="true" page="/403.html" />  <%
            return;
        }
    }
    
%>
<html>
  <head>
    <title>Usuário</title>
  <!--
	-->
	<style type="text/css">
	@import url(cssForm/style_form.css);
	</style>
  </head>
  
<body bgcolor='gray'>

<jsp:useBean class="org.siscoh.controller.Constants" id="constants" scope="page" />
<%
    if(editar)
    {
%>
    <form method="POST" action="Controlador?action=Usuario&cod=${constants.change}&userid=${param.userid}" onsubmit="">
<%
    } else {
%>
    <form method="POST" action="Controlador?action=Usuario&cod=${constants.insertUsuario}" onsubmit="">
<%
   }
%>        
  <!--<div style="border: 1px solid black; width: 120px; padding: 0px; margin: 0px; ">-->
<br>
<table id="todoform" cellspacing="5" rules="" border="0">
    <tr>
        <td colspan="2" bgcolor="">
            <div id="" align="center">
                <%
                    if(!editar)
                        out.println("Cadastro ");
                    else
                        out.println("Edição ");
                %>
                de Funcionários</div>
        </td>
        <td></td>
    </tr>        
    <tr>
      <td>Nome:</td>
      <td><input name='nome' id='nome' type='text' size='50' value="<%=nome%>"></td>
    </tr>
    <tr>
      <td>Login:</td>
      <td><input name='novologin' id='novologin' type='text' maxlength='50' size='50' value="<%=login%>"></td>
    </tr>
    <%
        if(editar && !self)
        {
    %>
        <tr>
            <td>Nova Senha:</td>
            <td><input name='senha' id='senha' type='password' maxlength='50' size='50' onkeypress=""></td>
        </tr>
    <%
        }
        else if (editar && self)
        {
    %>
          <tr>
            <td>Senha:</td>
            <td><input name='oldsenha' id='oldsenha' type='password' maxlength='50' size='50' onkeypress=""></td>
          </tr><tr>
            <td>Nova Senha:</td>
            <td><input name='senha' id='senha' type='password' maxlength='50' size='50' onkeypress=""></td>
          </tr><tr>
            <td>Confirmacão de Senha:</td>
            <td><input name='confsenha' id='confsenha' type='password' maxlength='50' size='50' onkeypress=""></td>
          </tr>
     <%
        }
        else
        {
     %>
        <tr>
            <td>Senha:</td>
            <td><input name='senha' id='senha' type='password' maxlength='50' size='50' onkeypress=""></td>
            </tr> 
        <tr>
            <td>Confirmacão de Senha:</td>
            <td><input name='confsenha' id='confsenha' type='password' maxlength='50' size='50' onkeypress=""></td>
        </tr>  
     <%
        }
        if(admin)
        {
    %>
      <tr>
        <td>Administrador:</td>
        <td><input name='admin' type='radio' maxlength='50' size='50' value="true" <%=isAdminStr%> /> Sim
        <br />
        <input name='admin' type='radio' maxlength='50' size='50' value="false" <%=notAdminStr%> /> Não</td>
      </tr>  
  <%
        }
  %>
    <tr>
	<td colspan="2">
  	<div align="center"><input class="" type="submit" value="Salvar" ></div>
  	</td>
   </tr>
   
  </table>
  </form>
  <br />
  <div align="center">
    <c:if test="${param.status != null}">
        <c:if test="${param.status == 1}">
            <c:if test="${param.userid != null}">
                <span id="status" class="success">Dados de usuário alterados com sucesso!</span>
            </c:if>
            <c:if test="${param.userid == null}">
                <span id="status" class="success">Usuário cadastrada com sucesso!</span>
            </c:if>
            <script>
                hideStatus();
            </script>
        </c:if>
        <c:if test="${param.status != 1}">
            <c:if test="${param.userid != null}">
                <span id="status" class="error">Erro ao alterar dados de usuário!</span>
            </c:if>
            <c:if test="${param.userid == null}">
                <span id="status" class="error">Erro ao cadastrar usuário!</span>
            </c:if>
            <script>
                hideStatus();
            </script>
        </c:if>
    </c:if>
  </div>
  </body>
</html>	  
    
